// pages/modalUser/modalUser.js

//定义本页面
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //页面关联数组
    userArray: [{ "id": "1", "name": "服务台1", "checked": false, "isHide": true },
    { "id": "2", "name": "服务台2", "checked": false, "isHide": true },
    { "id": "3", "name": "服务台3", "checked": false, "isHide": true },
    { "id": "4", "name": "服务台4", "checked": false, "isHide": true },
    { "id": "5", "name": "服务台5", "checked": false, "isHide": true },
    { "id": "6", "name": "服务台6", "checked": false, "isHide": true },
    { "id": "7", "name": "服务台7", "checked": false, "isHide": true },
    { "id": "8", "name": "服务台8", "checked": false, "isHide": true },
    { "id": "9", "name": "服务台9", "checked": false, "isHide": true },
    { "id": "10", "name": "服务台10", "checked": false, "isHide": true },
    { "id": "11", "name": "服务台11", "checked": false, "isHide": true }
    ],
    searchValue:'',  //查询的用户名（关联页面数据）

    thisIdArray: [],    //已经打钩选取的id 数组   
    thisValueArray: [], //已经打钩选取的用户名  数组 

    searchLoding: false,  //搜索按钮是否显示loding状态     
    isLower:true,    //是否可以滚动加载的指标
    pageId:''        //当前处理页面id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    console.log("ooooooooooooo",options);
    let pageId = options.pageId;    //页面标识id
    
    //console.log(options);
    //获取跳转页面传递过来的需要调用的人员 id 数组 和 value数组
    let thisIdArray, thisValueArray;
    options.id.length < 1 ? thisIdArray = [] : thisIdArray = options.id.split(',');  
    options.value.length < 1 ? thisValueArray = [] : thisValueArray = options.value.split(',');

    let userArray = that.data.userArray;

    thisIdArray.forEach(function (item, index) {
      userArray.forEach(function (item2, index2){
        if (item == item2.id){
          userArray[index2].checked = true     //把打钩标志上
        }
      })
    })

    that.setData({
      userArray: userArray,
      thisIdArray: thisIdArray,
      thisValueArray: thisValueArray,
      pageId: pageId         //存储 页面标识id
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("隐藏了")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("卸载了")

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //scroll-view 滑动到底部
  lower: function (e) {
    /* wx.request({
      url: '',
    }) */
    console.log(this.data.isLower);
    let userArray = that.data.userArray;
    if (that.data.isLower == true){
      var addArray = [{ "id": "12", "name": "服务台1", "checked": false , "isHide": true},   //滚动添加的数据
        { "id": "13", "name": "服务台1", "checked": false , "isHide": true},
        { "id": "14", "name": "服务台1", "checked": false , "isHide": true},
        { "id": "15", "name": "服务台1", "checked": false , "isHide": true},
        { "id": "16", "name": "服务台1", "checked": false , "isHide": true},];
      var userArrayConcat = userArray.concat(addArray);  //userArrayConcat==添加滚动数据后的总数组
      wx.showNavigationBarLoading(); //在标题栏中显示加载
      that.setData({       //是否可以滚动加载的指标 ： 不能
        isLower: false
      })
      
      wx.showLoading({    //添加加载中
        title: '加载中',
        mask : true
      })
      setTimeout(function () {
        wx.hideNavigationBarLoading(); //在标题栏中隐藏加载
        wx.hideLoading();              //等待中消失
        that.setData({                 //是否可以滚动加载的指标  ： true
          isLower: true,
          userArray: userArrayConcat
        })

      },2000)
    }else{
      console.log("不能重复刷新")
    } 
    

  },
  //input 输入并查询
  formChange: function(e){
    //debugger;

    let inputValue = e.detail.value;    //输入框输入的内容
    let userArray = that.data.userArray;  //全部数组

    let hasIn = false;  //是否找到同等名字标识
    if (inputValue.length > 0){
      userArray.forEach(function(item){
        if (inputValue == item.name) {   //当搜索框输入等于列表li name 时添加到要显示的列表，再显示，不然就全部显示
          item.isHide = true
          hasIn = true;
        } else {
          item.isHide = false
        }
      })
      if (hasIn) {    //如果找到同等名字标识就实例化此数组，找不到就实例化 userArray能全部显示的数组
        that.setData({
          userArray: userArray
        })
      } else {
        userArray.forEach(function (item) {
          item.isHide = true
        })
        that.setData({
          userArray: userArray
        })
      }
    } 
  },
  //搜索
  /* formSearch: function(e){
    let searchValue = that.data.searchValue;
    that.setData({
      searchLoding: true,
      //searchValue: searchValue
    })
    console.log(searchValue);
    setTimeout(function () {
      that.setData({
        searchLoding: false,
      })      
    },2000)
  }, */
  radioChange: function (e) {
    //debugger;
    function sortNumber(a,b){  //排序比较
      return a - b
    }

    let oldIdArray = e.detail.value;   //已经打钩选取的id 数组
    let thisIdArray = oldIdArray.sort(sortNumber);  //当前选中的checkbox id数组(排序后)
    let thisValueArray = [];      //已选中radio的value数组

    let userArray = that.data.userArray;    //全部数据

    userArray.forEach(function(item){
      item.checked = false;    //checkbox置空
    }) 
    thisIdArray.forEach(function(item){
      userArray.forEach(function(item2){
        if (item == item2.id) {
          item2.checked = true;       //存储全部的数组 checked
          thisValueArray.push(item2.name);
        }
      })
    })

    that.setData({
      userArray: userArray,
      thisIdArray: thisIdArray,  //存储已经打钩选取的 id数组
      thisValueArray: thisValueArray
    });
  
  },
  //确定提交按钮
  modalUsersubmit:function(e){

    //console.log(thisId);
    let userArray = that.data.userArray;
    let thisId = that.data.thisIdArray;
    let thisValue = that.data.thisValueArray;
    let pageId = that.data.pageId;
    //console.log(getCurrentPages());

    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
      
    //console.log("thisId: " + thisId, "thisValue:" + thisValue);
    switch (pageId){
      case "nextActivityId":
        prevPage.setData({
          "nextActivityId.value": {
            "idArray": thisId,
            "nameArray": thisValue
          }
        })
        break
      case "nextActPersonId": 
        prevPage.setData({
          "nextActPersonId.value": {
            "idArray": thisId,
            "nameArray": thisValue
          }          
        }) 
        break  
    }
    wx.navigateBack({
      delta: 1,  //返回上一页
      success: function (ee) {
        console.log("ee", ee);
      }
    })


    /* wx.redirectTo({
      url: '../entryWant/entryWant?id=' + thisId + '&value=' + thisValue + '&pageId=' + pageId,
    }) */
  }
})